package de.QuizCommunity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.Facebook.ServiceListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

import de.QuizCommunity.util.AppVariabels;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Social_Channel extends Activity {
	

	Integer i =0;
	protected static JSONArray jsonArray;
	Facebook facebook = AppVariabels.facebook;

	RequestListener rL = new RequestListener() {
		
		public void onMalformedURLException(MalformedURLException e, Object state) {
			// TODO Auto-generated method stub
			
		}
		
		public void onIOException(IOException e, Object state) {
			// TODO Auto-generated method stub
			
		}
		
		public void onFileNotFoundException(FileNotFoundException e, Object state) {
			// TODO Auto-generated method stub
			
		}
		
		public void onFacebookError(FacebookError e, Object state) {
			// TODO Auto-generated method stub
			
		}
		
		public void onComplete(String response, Object state) {
			// TODO Auto-generated method stub
			
		}
	};
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        

        setContentView(R.layout.social_channel);
        final int frageID = getIntent().getIntExtra("frageID",0);
        
        final Button btn = (Button) findViewById(R.id.btnFrageFbJoker);
        Button btnZur = (Button)findViewById(R.id.btnFbJokerZurueck);
        final TextView tv = (TextView)findViewById(R.id.tvFrageFBjoker);
    	ImageView iv = (ImageView)findViewById(R.id.ivRefresh);
        tv.setText(AppVariabels.frageAntwortFbJoker);
        if(AppVariabels.jokerUsed ==false){

            btn.setEnabled(true);
        }
               
        	
        	iv.setOnClickListener( new View.OnClickListener() {
        	
				@Override
				public void onClick(View v) {
					 tv.setText(AppVariabels.frageAntwortFbJoker);

		        	 final List valueList = new ArrayList<String>();

		             final ListView lv = (ListView)findViewById(R.id.lvFb);
		             
		        	 JSONObject jo;
		      		try {
		      			 try {
		      				jo = Util.parseJson( facebook.request("me/statuses",new Bundle(), "GET"));
		      				JSONArray jArray=(JSONArray)jo.get("data");
		      				
		      				
		      	            for(int i=0;i<(jArray.length());i++)
		      	            {
		      	            	

		      	                jArray.getJSONObject(i).getJSONObject("from").get("name");   

		      	                if(jArray.getJSONObject(i).get("message").toString().contains(AppVariabels.fragestellungFbJoker)){
		      	                	if(jArray.getJSONObject(i).has("comments")){
		      	                	if (jArray.getJSONObject(i).getJSONObject("comments").has("data")){
		      	                		
		      	                	JSONArray  comments =(JSONArray)  jArray.getJSONObject(i).getJSONObject("comments").get("data");
		      	                		for(int k=0; k<(comments.length());k++){
		      	                			String co = comments.getJSONObject(k).get("message").toString();
		      	    	 	                String names =   comments.getJSONObject(k).getJSONObject("from").get("name").toString();

		      	    	 	                valueList.add("Von: " + names + "\n " + "AW: " + co);
		      	                		}
		      	 	               	

		      	                		}
		      	                	}
		      	                	else{
		      	                			if(!valueList.contains("keine Antworten der Community") && valueList.size()==0){
		      	                			valueList.add("keine Antworten der Community");
		      	                			}
		      	                	}
		      	                }
		      	            }   
		      	            
		      	            
		      			} catch (FacebookError e1) {
		      				// TODO Auto-generated catch block
		      				e1.printStackTrace();
		      			} catch (JSONException e1) {
		      				// TODO Auto-generated catch block
		      				e1.printStackTrace();
		      			}
		      		} catch (FileNotFoundException e1) {
		      			// TODO Auto-generated catch block
		      			e1.printStackTrace();
		      		} catch (MalformedURLException e1) {
		      			// TODO Auto-generated catch block
		      			e1.printStackTrace();
		      		} catch (IOException e1) {
		      			// TODO Auto-generated catch block
		      			e1.printStackTrace();
		      		}		
		              
		      		final ListAdapter adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, valueList);
		      		lv.setAdapter(adapter);
					
				}
            	 
            	 
             });
            
        


        btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		        AppVariabels.jokerUsed= true;

				
				try{
                    Bundle parameters = new Bundle();
                    parameters.putString("message", AppVariabels.frageAntwortFbJoker);
                    parameters.putString("description", "Bitte beantwortet mir diese Frage");
                    String response = facebook.request("me/feed", parameters, "POST");
                    Toast.makeText(getApplicationContext(), "Frage gesendet", Toast.LENGTH_LONG).show();
                    btn.setEnabled(false);
                    
                } catch (Exception e) {
                    e.printStackTrace();
                }
            
    				
			}
		});
        
        btnZur.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
		    	Intent in = new Intent(Social_Channel.this,Frage.class);
		    	in.putExtra("frageID", frageID);
		    	startActivity(in);
				
			}
		});
        

    			

    }
}
